package com.skku.selab.fl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;

import java.util.ArrayList;

public class LineCounter
{
	ArrayList<String> statementList = null;

    public ArrayList<String> getStatementList() {
		return statementList;
	}

	public int count(File file)
    {
        int count = 0;
        // Get all files in current directory
        if (file.isDirectory())
        {
            // Get the listing in this directory
            count = recurseDirectory(file, count);
        }
        else
        {
            // It is a file
            count = countLinesIn(file);
        }
        
        return count;
    }

    /** 
     * Counts code excluding comments and blank lines in the given file
     * @param file
     * @return int
     */
    public int countLinesIn(File file)
    {
    	statementList = new ArrayList<String>();
    	
        int count = 0;
        try
        {
            BufferedReader reader = new BufferedReader(new FileReader(file.getAbsolutePath()));
            String line = null;
            String stement = null;
            do
            {
                line = reader.readLine();
                
                if(line != null && line.indexOf("\"") > -1){
                	line = line.replaceAll("\"", " ");
                }
                
                stement = "\"" + "   " + line + "\"";
            	statementList.add(stement);
            }while (line != null);
            reader.close();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return count;
    }

    public int recurseDirectory(File file, int count)
    {
        File[] files = file.listFiles(new FileFilter()
        {
            public boolean accept(File file)
            {
                if (file.getName().indexOf(".java") != -1
                    || file.isDirectory())
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        });
        for (int i = 0; i < files.length; i++)
        {
            count += count(files[i]);
        }
        return count;
    }

    public static void main(String[] args)
    {
        LineCounter lc = new LineCounter();
        System.out.println("Line Count = " + lc.count(new File("C:\\Users\\Gold_Youn\\Desktop\\LGProject\\dev\\TestProject\\src\\beans\\LineCounter.java")));
    }
}